<?php
// $Id: shop.php 543 2012-10-15 10:18:37Z tomqin@gmail.com $

class Shop_Action extends Action_Front{
    public $shop_id;

    public function __construct(){
        $this->route = 1;//路由1
    }

    function init(){
        $this->shop_id = intval($this->params);
        if($this->shop_id == 1003){
            header('Location: http://lihua.sudingke.com');
            exit;
        }
        $cityid  = Db::getFirst("SELECT city_id FROM `biz_shop` WHERE shop_id = '{$this->shop_id}'");
        if(!$cityid){
            jumpto('/waimai');exit;
        }
        if($cityid != $this->cityid){
            $cityids = App::getConfig('cityids');
            if(isset($cityids)){
                $url = 'http://'. $cityids[$cityid]. '.sudingke.com'.$_SERVER['REQUEST_URI'];
                jumpto($url, true);
            }else{
                jumpto('/waimai');exit;
            }
        }
        $methods = explode('/', $this->method, 2);
        $this->method = $methods[0];
        $this->params = isset($methods[1]) ? $methods[1] : '';
    }

    function onAjax(){
        $type = $value = '';
        @list($type, $value) = explode('-', $this->params);
        if($type == 'dish'){
            $dish_id = intval($value);
            $data = Db::getRow("SELECT dish_id, name, price FROM `biz_dish` WHERE shop_id='{$this->shop_id}' AND dish_id='{$value}'");
            if($data){
                $data += array('success' => true);
            }else{
                $data = array('success' => false);
            }
            echo json_encode($data);
        }
    }

    function onReview(){
        $shop   = $this->getWaimai($this->shop_id);
        $dishes = array();
        if($shop){
            //区域
            $zone = App::module('zone', '', array($this->cityid));
            $bigzone  = $zone->getZoneById($shop['zone_id']);
            $subzone  = $zone->getZoneById($shop['subzone_id']);

            //seo信息
            $seo_name = $shop['name'];
            $seo_zone = $bigzone->name.$subzone->name;
            $title       = $shop['seo_title'] ? $shop['seo_title'] : "{$seo_name}外卖网_外卖电话_外卖信息_{$seo_zone}快餐外卖送餐_网上订餐";
            $keywords    = $shop['seo_keywords'] ? $shop['seo_keywords'] : preg_replace('/[\|_ -,]/', ',', $title);
            $description = $shop['seo_description'] ? $shop['seo_description'] : $title.',区域:'.$seo_zone.',地址:'.$shop['address'].',订餐时间:'.$shop['delivery_time'].'_订餐范围:'.$shop['delivery_desc'];
            $this->seoContent($title, $keywords, $description);
        }else{
            jumpto('/waimai');exit;
        }

        //订餐电话
        if($shop['call_number']){
            $order_call = '400 0411 569 转 '.$shop['call_number'];
        }else{
            if($shop['status'] >= 2){
                $order_call = '400 0411 569';
            }else{
                $order_call = $shop['telphone'];
            }
        }

        $this->setVar('dish_count',  0);
        $this->setVar('order_call',  $order_call);
        $this->setVar('shop', $shop);
        $this->setVar('cityinfo', App::getCityInfo());
        $this->setVar('bigzone', $bigzone);
        $this->setVar('subzone', $subzone);

        $this->render('shop/review-index');
    }

    function onIndex(){
        $method = '_'.$this->method.'_'.$this->params;
        if(!method_exists($this, $method)){
            $method = '_index_auto';
        }
        $this->cachename = join('|', array($this->cityid, $this->action, $this->method, $method, $this->shop_id));

        $force = array_get_bool($_GET, 'force');
        $this->showCache($this->cachename, array($this, $method), 86400, $force);
    }

    function _index_list(){
        $this->_index_auto('list');
    }

    function _index_foto(){
        $this->_index_auto('foto');
    }

    function _index_auto($mode = ''){
        $shop   = $this->getWaimai($this->shop_id);
        $dishes = array();
        if($shop){
            $cats   = $this->getCats($this->shop_id);
            $dishes = $this->getDishes($this->shop_id, $cats, $dish_count);
            if(isset($dishes[0])) $cats[0] = '默认分类';
            if(!$mode){ //test for `foto` or `list`
                $a = $b = 0;
                foreach($dishes as $items){
                    foreach($items as $item){
                        $b ++;
                        if(!empty($item['picture'])) $a ++;
                    }
                }
                $mode = ($a > $b * 0.7) ?  'foto' : 'list';
            }

            //区域
            $zone = App::module('zone', '', array($this->cityid));
            $bigzone  = $zone->getZoneById($shop['zone_id']);
            $subzone  = $zone->getZoneById($shop['subzone_id']);

            //seo信息
            $seo_name = $shop['name'];
            $seo_zone = $bigzone->name.$subzone->name;
            $title       = $shop['seo_title'] ? $shop['seo_title'] : "{$seo_name}外卖网_外卖电话_外卖信息_{$seo_zone}快餐外卖送餐_网上订餐";
            $keywords    = $shop['seo_keywords'] ? $shop['seo_keywords'] : preg_replace('/[\|_ -,]/', ',', $title);
            $description = $shop['seo_description'] ? $shop['seo_description'] : $title.',区域:'.$seo_zone.',地址:'.$shop['address'].',订餐时间:'.$shop['delivery_time'].'_订餐范围:'.$shop['delivery_desc'];
            $this->seoContent($title, $keywords, $description);
        }else{
            jumpto('/waimai');exit;
        }

        if($mode != 'foto'){
            $mode = 'list';
        }

        //订餐电话
        if($shop['call_number']){
            $order_call = '400 0411 569 转 '.$shop['call_number'];
        }else{
            if($shop['status'] >= 2){
                $order_call = '400 0411 569';
            }else{
                $order_call = $shop['telphone'];
            }
        }

        $this->setVar('order_call',  $order_call);
        $this->setVar('mode', $mode);
        $this->setVar('shop', $shop);
        $this->setVar('cats', $cats);
        $this->setVar('dish_count', $dish_count);
        $this->setVar('cityinfo', App::getCityInfo());
        $this->setVar('bigzone', $bigzone);
        $this->setVar('subzone', $subzone);
        $this->setVar('dishes', $dishes);

        if($shop['status'] == 3)
            $this->render('shop/dingcan-index');
        else
            $this->render('shop/waimai-index');
    }

    function onPreview(){
        $this->_index(true);
    }

    protected function getWaimai($shop_id){
        return Db::getRow("SELECT wm.*, se.*, s.* FROM `biz_shop` s LEFT JOIN `biz_waimai` wm ON wm.shop_id = s.shop_id LEFT JOIN `biz_shop_extra` se ON se.shop_id = s.shop_id WHERE s.shop_id = '{$shop_id}' AND s.shop_type = 1");
    }

    public function getCats($shop_id){
        $shop_id = intval($shop_id);
        $res = Db::getKeyArray("SELECT dish_cat, cat_name FROM `biz_dish_cat` WHERE `shop_id` = '{$shop_id}' ORDER BY ordnum DESC", 'dish_cat', 'cat_name');
        return $res ? $res : array();
    }

    protected function getDishes($shop_id, $dish_cats, &$dish_count){
        $dish_count = 0;
        $list = array();
        $stmt = Db::query("SELECT * FROM `biz_dish` WHERE `shop_id` = '{$shop_id}' ORDER BY ordnum DESC");
        while($row = $stmt->fetch_assoc()){
            if(isset($dish_cats[$row['dish_cat']])){
                $list[$row['dish_cat']][] = $row;
            }else{
                $list[0][] = $row;
            }
            $dish_count ++;
        }
        return $list;
    }
/*
    protected function getHotelMenu($shop_id){
        $res = Db::getAll("SELECT * FROM `biz_hotel_menu` WHERE shop_id = '{$shop_id}' ORDER BY ordnum DESC");
        return $res ? $res : array();
    }

    protected function getHotelAlbum($shop_id){
        $res = Db::getAll("SELECT * FROM `biz_hotel_album` WHERE shop_id = '{$shop_id}' ORDER BY ordnum DESC");
        return $res ? $res : array();
    }

    protected function getRelateHotel($num){
        $num = intval($num);
        $res = Db::getAll("SELECT shop_id, picture, name, list_data FROM `biz_shop` WHERE shop_type = 2 AND shop_id <> {$this->shop_id} ORDER BY rand() Limit {$num}");
        return $res ? $res : array();
    }
*/
}
?>